﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

using System.Data;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;

using System.Diagnostics;

namespace doris.Admin {
    public partial class MailRespons : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {
            if (Page.IsPostBack) {
                Debug.WriteLine("Postback");
                MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
                try {
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand();
                    cmd.Connection = conn;

                    //          Console.WriteLine("Connecting to MySQL...");
                    //                cmd.CommandText = "getTicketStatus";
                    cmd.CommandText = "storeDefaultResponse";
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new MySqlParameter("@in_message", MySqlDbType.Text));
                    cmd.Parameters["@in_message"].Value = Server.HtmlEncode(Editor.Text);
                    cmd.Parameters["@in_message"].Direction = ParameterDirection.Input;
                    cmd.ExecuteNonQuery();

                    cmd.Dispose();
                } catch (MySql.Data.MySqlClient.MySqlException ex) {
                    Debug.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
                } finally {
                    conn.Close();
                }

            } else {
                Debug.WriteLine("NOT Postback");
                MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
                try {
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand();
                    cmd.Connection = conn;

                    //          Console.WriteLine("Connecting to MySQL...");
                    //                cmd.CommandText = "getTicketStatus";
                    cmd.CommandText = "getDefaultResponse";
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new MySqlParameter("@out_message", MySqlDbType.Text));
                    cmd.Parameters["@out_message"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    Editor.Text = Server.HtmlDecode(cmd.Parameters["@out_message"].Value.ToString());

                    cmd.Dispose();
                } catch (MySql.Data.MySqlClient.MySqlException ex) {
                    Debug.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
                } finally {
                    conn.Close();
                }
            }
        }
    }
}